package com.eipc;

import org.androidpn.sdk.MainService;
import org.androidpn.sdk.ServiceManager;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class EipcMainService extends Service {
	private static ServiceManager mServiceManager;
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		mServiceManager = new ServiceManager(this);
		new Thread(new Runnable() {
			@Override
			public void run() {
				Log.e("-----------startFetchData-----------------","startFetchData");
				startFetchNotice();
			}
		}).start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				Log.e("-----------startFushLogin-----------------","startFushLogin");
				startReloginPush();
			}
		}).start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				Log.e("-----------startFetchConsult-----------------","startFetchConsult");
				startFetchConsult();
			}
		}).start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				Log.e("-----------startFetchWeather-----------------","startFetchWeather");
				startFetchWeather();
			}
		}).start();
		
		new Thread(new Runnable() {
            @Override
            public void run() {
            	Log.e("-----------startFetchReply-----------------","startFetchReply");
            	startFetchReply();
            }
        }).start();
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		stopFetchNotice();
		stopReloginPush();
		stopFetchReply();
		stopFetchWeather();
		stopFetchConsult();
		Log.e("-----------stop eipc main service-----------------","startFushLogin");
	}
	
	private void startFetchNotice(){
		Intent intent = new Intent(this, FetchNoticeService.class);
		PendingIntent sender = PendingIntent.getService(this,1,intent,0);
		AlarmManager am = (AlarmManager)this.getSystemService(Activity.ALARM_SERVICE);
		am.setRepeating(AlarmManager.RTC,System.currentTimeMillis()+6000,this.getResources().getInteger(R.integer.fetch_data_interval)*60*1000,sender);
	}
	private void startFetchConsult(){
		Intent intent = new Intent(this, FetchConsultService.class);
		PendingIntent sender = PendingIntent.getService(this,1,intent,0);
		AlarmManager am = (AlarmManager)this.getSystemService(Activity.ALARM_SERVICE);
		am.setRepeating(AlarmManager.RTC,System.currentTimeMillis()+30000,this.getResources().getInteger(R.integer.fetch_data_interval)*60*1000,sender);
	}
	private void startFetchWeather(){
		Intent intent = new Intent(this, FetchWeatherService.class);
		PendingIntent sender = PendingIntent.getService(this,1,intent,0);
		AlarmManager am = (AlarmManager)this.getSystemService(Activity.ALARM_SERVICE);
		am.setRepeating(AlarmManager.RTC,System.currentTimeMillis()+60000,this.getResources().getInteger(R.integer.fetch_data_interval)*60*1000,sender);
	}
	private void startFetchReply(){
		Intent intent = new Intent(this, FetchReplyService.class);
		PendingIntent sender = PendingIntent.getService(this,1,intent,0);
		AlarmManager am = (AlarmManager)this.getSystemService(Activity.ALARM_SERVICE);
		am.setRepeating(AlarmManager.RTC,System.currentTimeMillis()+90000,30000,sender);
	}
	private void startReloginPush(){
        Intent intent = MainService.getIntent();
		PendingIntent sender = PendingIntent.getService(this,1,intent,0);
		AlarmManager am = (AlarmManager)this.getSystemService(Activity.ALARM_SERVICE);
		am.setRepeating(AlarmManager.RTC,System.currentTimeMillis()+500,60*1000*10,sender);
    }
	
	private void stopFetchReply(){
		Intent intent = new Intent(this, FetchReplyService.class);
		PendingIntent sender = PendingIntent.getService(this, 1, intent, 0);
		AlarmManager am = (AlarmManager)this.getSystemService(Activity.ALARM_SERVICE);
		am.cancel(sender);
	}
	
	private void stopFetchWeather(){
		Intent intent = new Intent(this, FetchWeatherService.class);
		PendingIntent sender = PendingIntent.getService(this, 1, intent, 0);
		AlarmManager am = (AlarmManager)this.getSystemService(Activity.ALARM_SERVICE);
		am.cancel(sender);
	}
	
	private void stopFetchConsult(){
		Intent intent = new Intent(this, FetchConsultService.class);
		PendingIntent sender = PendingIntent.getService(this, 1, intent, 0);
		AlarmManager am = (AlarmManager)this.getSystemService(Activity.ALARM_SERVICE);
		am.cancel(sender);
	}
	private void stopFetchNotice(){
		Intent intent = new Intent(this, FetchNoticeService.class);
		PendingIntent sender = PendingIntent.getService(this, 1, intent, 0);
		AlarmManager am = (AlarmManager)this.getSystemService(Activity.ALARM_SERVICE);
		am.cancel(sender);
	}
	    
	 private void stopReloginPush(){
	    Intent intent = MainService.getIntent();
	    PendingIntent sender = PendingIntent.getService(this, 1, intent, 0);
	    AlarmManager am = (AlarmManager)this.getSystemService(Activity.ALARM_SERVICE);
	    am.cancel(sender);
	 }
}
